
package gui.panels;

import gui.MainFrame;
import gui.listeners.GuiActionEvent;
import gui.listeners.GuiActionEventData;
import gui.listeners.GuiActionEventType;
import gui.model.DrinkModel;
import gui.panels.components.MmiButton;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 *
 * @author JtR
 */
public class SelectBrewTimePanel extends GuiPanel {
    private MmiButton doneButton, tempPlusButton, tempMinusButton, timePlusButton, timeMinusButton;
    private JLabel brewTimeLabel, temperatureLabel;
    private Image temp70, temp75, temp80, temp85, temp90, temp95, temp100;
    private Image time3, time4, time5, time6, time7, time8, time9, time10;
    
    public SelectBrewTimePanel(MainFrame parent) {
        super(parent, BackgroundType.TEA);
    }
    
    @Override
    protected void initComponents() {
        Dimension tempSize = new Dimension(160, 510);
        Dimension timeSize = new Dimension(423, 409);
        temperatureLabel = new JLabel();
        constraints.insets = new Insets(73, 286, 0, 0);
        temperatureLabel.setPreferredSize(tempSize);
        add(temperatureLabel, constraints);

        brewTimeLabel = new JLabel();
        constraints.insets = new Insets(58, 381, 0, 0);
        brewTimeLabel.setPreferredSize(timeSize);
        add(brewTimeLabel, constraints);

        doneButton = new MmiButton("res/teetime/fertigTee.png", 140);
        doneButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fireGuiActionEvent(new GuiActionEvent(GuiActionEventType.TEA_DONE));
            }
        });
        constraints.anchor = GridBagConstraints.SOUTHEAST;
        constraints.insets = new Insets(0, 0, 20, 20);
        add(doneButton, constraints);
        constraints.anchor = GridBagConstraints.NORTHWEST;
            
        try {
            time3 = ImageIO.read(this.getClass().getClassLoader().getResource("res/teetime/3mins.png")).getScaledInstance(timeSize.width, timeSize.height, Image.SCALE_SMOOTH);
            time4 = ImageIO.read(this.getClass().getClassLoader().getResource("res/teetime/4mins.png")).getScaledInstance(timeSize.width, timeSize.height, Image.SCALE_SMOOTH);
            time5 = ImageIO.read(this.getClass().getClassLoader().getResource("res/teetime/5mins.png")).getScaledInstance(timeSize.width, timeSize.height, Image.SCALE_SMOOTH);
            time6 = ImageIO.read(this.getClass().getClassLoader().getResource("res/teetime/6mins.png")).getScaledInstance(timeSize.width, timeSize.height, Image.SCALE_SMOOTH);
            time7 = ImageIO.read(this.getClass().getClassLoader().getResource("res/teetime/7mins.png")).getScaledInstance(timeSize.width, timeSize.height, Image.SCALE_SMOOTH);
            time8 = ImageIO.read(this.getClass().getClassLoader().getResource("res/teetime/8mins.png")).getScaledInstance(timeSize.width, timeSize.height, Image.SCALE_SMOOTH);
            time9 = ImageIO.read(this.getClass().getClassLoader().getResource("res/teetime/9mins.png")).getScaledInstance(timeSize.width, timeSize.height, Image.SCALE_SMOOTH);
            time10 = ImageIO.read(this.getClass().getClassLoader().getResource("res/teetime/10mins.png")).getScaledInstance(timeSize.width, timeSize.height, Image.SCALE_SMOOTH);
            
            temp70 = ImageIO.read(this.getClass().getClassLoader().getResource("res/teetime/70c.png")).getScaledInstance(tempSize.width, tempSize.height, Image.SCALE_SMOOTH);
            temp75 = ImageIO.read(this.getClass().getClassLoader().getResource("res/teetime/75c.png")).getScaledInstance(tempSize.width, tempSize.height, Image.SCALE_SMOOTH);
            temp80 = ImageIO.read(this.getClass().getClassLoader().getResource("res/teetime/80c.png")).getScaledInstance(tempSize.width, tempSize.height, Image.SCALE_SMOOTH);
            temp85 = ImageIO.read(this.getClass().getClassLoader().getResource("res/teetime/85c.png")).getScaledInstance(tempSize.width, tempSize.height, Image.SCALE_SMOOTH);
            temp90 = ImageIO.read(this.getClass().getClassLoader().getResource("res/teetime/90c.png")).getScaledInstance(tempSize.width, tempSize.height, Image.SCALE_SMOOTH);
            temp95 = ImageIO.read(this.getClass().getClassLoader().getResource("res/teetime/95c.png")).getScaledInstance(tempSize.width, tempSize.height, Image.SCALE_SMOOTH);
            temp100 = ImageIO.read(this.getClass().getClassLoader().getResource("res/teetime/100c.png")).getScaledInstance(tempSize.width, tempSize.height, Image.SCALE_SMOOTH);
        } catch (IOException ex) {
            Logger.getLogger(SelectBrewTimePanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        tempMinusButton = new MmiButton("res/teetime/-.png", 150);
        Font f = tempMinusButton.getFont().deriveFont(50f);
        tempMinusButton.setFont(f);
        tempMinusButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(DrinkModel.getInstance().getTemperature() > 70)
                    DrinkModel.getInstance().setTemperature(DrinkModel.getInstance().getTemperature() - 5);
                updateFields();
            }
        });
//        tempMinusButton.setPreferredSize(new Dimension(150, 150));
        constraints.insets = new Insets(375, 50, 0, 0);
        add(tempMinusButton, constraints);
        
        tempPlusButton = new MmiButton("res/teetime/+.png", 150);
        tempPlusButton.setFont(f);
        tempPlusButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(DrinkModel.getInstance().getTemperature() < 100)
                    DrinkModel.getInstance().setTemperature(DrinkModel.getInstance().getTemperature() + 5);
                updateFields();
            }
        });
//        tempPlusButton.setPreferredSize(new Dimension(150, 150));
        constraints.insets = new Insets(200, 50, 0, 0);
        add(tempPlusButton, constraints);
        
        timeMinusButton = new MmiButton("res/teetime/-.png", 150);
        timeMinusButton.setFont(f);
        timeMinusButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(DrinkModel.getInstance().getBrewTime() > 3)
                    DrinkModel.getInstance().setBrewTime(DrinkModel.getInstance().getBrewTime() - 1);
                updateFields();
            }
        });
//        timeMinusButton.setPreferredSize(new Dimension(150, 150));
        constraints.insets = new Insets(375, 800, 0, 0);
        add(timeMinusButton, constraints);
        
        timePlusButton = new MmiButton("res/teetime/+.png", 150);
        timePlusButton.setFont(f);
        timePlusButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(DrinkModel.getInstance().getBrewTime() < 10)
                    DrinkModel.getInstance().setBrewTime(DrinkModel.getInstance().getBrewTime() + 1);
                updateFields();
            }
        });
//        timePlusButton.setPreferredSize(new Dimension(150, 150));
        constraints.insets = new Insets(200, 800, 0, 0);
        add(timePlusButton, constraints);
    }

    @Override
    public String getDescription() {
        return "Bitte wählen Sie Brühzeit und Temperatur";
    }

    @Override
    public void updateFields() {
        if(DrinkModel.getInstance().getBrewTime() == 0) {
            DrinkModel.getInstance().setBrewTime(5);
        }
        
        if(DrinkModel.getInstance().getTemperature() == 0) {
            DrinkModel.getInstance().setTemperature(80);
        }
        
        updateBrewTime();
        updateTemperature();
        
        timeMinusButton.setEnabled(true);
        timePlusButton.setEnabled(true);
        tempMinusButton.setEnabled(true);
        tempPlusButton.setEnabled(true);
        
        if(DrinkModel.getInstance().getBrewTime() == 3)
            timeMinusButton.setEnabled(false);
        if(DrinkModel.getInstance().getBrewTime() == 10)
            timePlusButton.setEnabled(false);
        if(DrinkModel.getInstance().getTemperature() == 70)
            tempMinusButton.setEnabled(false);
        if(DrinkModel.getInstance().getTemperature() == 100)
            tempPlusButton.setEnabled(false);
    }
    
    private void updateTemperature() {
        switch(DrinkModel.getInstance().getBrewTime()) {
            case 3:
                brewTimeLabel.setIcon(new ImageIcon(time3));
                break;
            case 4:
                brewTimeLabel.setIcon(new ImageIcon(time4));
                break;
            case 5:
                brewTimeLabel.setIcon(new ImageIcon(time5));
                break;
            case 6:
                brewTimeLabel.setIcon(new ImageIcon(time6));
                break;
            case 7:
                brewTimeLabel.setIcon(new ImageIcon(time7));
                break;
            case 8:
                brewTimeLabel.setIcon(new ImageIcon(time8));
                break;
            case 9:
                brewTimeLabel.setIcon(new ImageIcon(time9));
                break;
            case 10:
                brewTimeLabel.setIcon(new ImageIcon(time10));
                break;
        }
        
        switch(DrinkModel.getInstance().getTemperature()) {
            case 70:
                temperatureLabel.setIcon(new ImageIcon(temp70));
                break;
            case 75:
                temperatureLabel.setIcon(new ImageIcon(temp75));
                break;
            case 80:
                temperatureLabel.setIcon(new ImageIcon(temp80));
                break;
            case 85:
                temperatureLabel.setIcon(new ImageIcon(temp85));
                break;
            case 90:
                temperatureLabel.setIcon(new ImageIcon(temp90));
                break;
            case 95:
                temperatureLabel.setIcon(new ImageIcon(temp95));
                break;
            case 100:
                temperatureLabel.setIcon(new ImageIcon(temp100));
                break;
        }
    }
    
    private void updateBrewTime() {
        
    }

    @Override
    public boolean getPreviousButtonEnabled() {
        return true;
    }

    @Override
    public boolean getNextButtonEnabled() {
        return true;
    }

    @Override
    public String getName() {
        return "BREW_TIME_NAME";
    }

    @Override
    public GuiPanel getNextPanel() {
        return parent.finishPanel;
    }

    @Override
    public GuiPanel getPreviousPanel() {
        return parent.selectSizePanel;
    }
}
